// 本c语言源码用于展示步兵在不同时间段dps情况，考虑了4种步兵情况（迫击炮步兵，99式步兵，反坦克步枪步兵和11式轻机枪手）
//以及地堡强化下时间因素
//最后图形绘制有问题需要改进
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define UNIT_NUM 4

char *unit_names[UNIT_NUM] = {"迫击炮步兵", "99式步兵", "11式轻机枪手", "反坦克步枪步兵"};
float dps_values[UNIT_NUM];

float get_single_damage(char *damage_type) {
    char *unit_name;
    float damage;

    for (int i = 0; i < UNIT_NUM; i++) {
        unit_name = unit_names[i];

        if (strcmp(unit_name, damage_type) == 0) {
            switch (i) {
                case 0:
                    damage = 26.0;
                    break;
                case 1:
                    damage = 19.0;
                    break;
                case 2:
                    damage = 8.0;
                    break;
                case 3:
                    damage = 8.0;
                    break;
                default:
                    damage = 0.0;
                    break;
            }
//对应步兵对建筑的5种伤害情况
            return damage;
        }
    }

    return 0.0;
}

void calculate_dps(int t, int armor, int skill_time) {
    int i;
    float building_damage, fire_rate, dps;
    
    if (t < 260) {
        armor = 17;
    } else if (t < 400) {
        armor += 3;
    } else {
        armor += 3;
    }

    if (skill_time < t && t < skill_time + 60) {
        armor += 5;
    }

    for (i = 0; i < UNIT_NUM; i++) {
        building_damage = 0.0;

        switch (i) {
            case 0:
                building_damage = 588.0;
                fire_rate = 0.8;
                break;
            case 1:
                building_damage = 437.0;
                fire_rate = 0.9;
                break;
            case 2:
                building_damage = 237.0;
                fire_rate = 1.6;
                break;
            case 3:
                building_damage = 237.0;
                fire_rate = 1.6;
                break;
        }
//对应地堡护甲（科研中心：防御设施改进）增加情况，考虑时间因素
        dps = fmax((building_damage - armor) / fire_rate, 0.0);
        dps_values[i] = dps;
//dps计算公式
    }
}

void plot_graph(int *t_values, float *dps_values, int time_len) {
    FILE *fp = popen("gnuplot -persist", "w");

    fprintf(fp, "set title 'DPS vs. T'\n");
    fprintf(fp, "set xlabel 't'\n");
    fprintf(fp, "set ylabel 'dps'\n");
    fprintf(fp, "plot '-' with lines\n");

    for(int i = 0; i < time_len; i++) {
        fprintf(fp, "%d %lf\n", t_values[i], dps_values[i]);
    }

    fprintf(fp, "e\n");

    pclose(fp);
}

int main() {
    int t, armor = 17, skill_time = 0;
    int time_len = 1200;
    int t_values[time_len];
    float total_dps;

    // 生成时间范围
    for (int i = 0; i < time_len; i++) {
        t_values[i] = i + 1;
    }

    printf("请输入时间：");
    scanf("%d", &t);

    // 计算 DPS
    calculate_dps(t, armor, skill_time);

    // 计算总 DPS
    total_dps = 0.0;
    for (int i = 0; i < UNIT_NUM; i++) {
        total_dps += dps_values[i];
    }

    printf("%d秒时的总DPS为：%f\n", t, total_dps);

    // 绘制图形
    plot_graph(t_values, dps_values, time_len);

    return 0;
}
